﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace BlogOutModell
{
    [MetadataType(typeof(PostMetaData))]
    public partial class Post
    {
        
    }
    public class PostMetaData
    {
        [Required]
        public string Title { get; set; }
        [Required]
        public string Content { get; set; }
    }

    public class PostDB
    {
        public static readonly BlogOutModelContainer Context = DB.Context;

        public static Post GetPost(int id)
        {
            return (Context.Posts.Where(p => p.Id == id && !p.IsDeleted)).SingleOrDefault();
        }

        public static Post GetPostThatIsDeleted(int id)
        {
            return (Context.Posts.Where(p => p.Id == id)).SingleOrDefault();
        }

        public static List<Post> GetAllPosts()
        {
            return Context.Posts.Where(p => !p.IsDeleted).ToList();
        }

        public static List<Post> GetAllPostsByBlogId(int id)
        {
            List<Post> result = (Context.Posts.Where(p => p.BlogId == id && !p.IsDeleted)).ToList();
            return result;
        }

        public static bool DoesUserOwnsPost(int userId, int postId)
        {
            Post post = GetPost(postId);
            bool result = post.Blog.User.Id == userId;
            return result;
        }
    }
}